package com.swp.oa.sys.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swp.oa.sys.dao.IDepartmentDao;
import com.swp.oa.sys.dao.IDepartmentUserDao;
import com.swp.oa.sys.dto.domian.DepartmentDto;
import com.swp.oa.sys.dto.page.EasyuiTreeNode;
import com.swp.oa.sys.pojo.Department;
import com.swp.oa.sys.pojo.DepartmentExample;
import com.swp.oa.sys.pojo.DepartmentUser;
import com.swp.oa.sys.pojo.DepartmentUserKey;
import com.swp.oa.sys.service.IDepartmentService;
import com.swp.oa.util.DateUtil;

@Service("departmentService")
public class DepartmentServiceImpl implements IDepartmentService{

	@Autowired
	private IDepartmentDao departmentDao;
	
	@Autowired
	private IDepartmentUserDao departmentUserDao;
	
	@Override
	public boolean save(Department dep) {
		dep.setCreatedate(new Date());
		dep.setUpdatedate(new Date());
		departmentDao.insertSelective(dep);
		return false;
	}

	@Override
	public boolean save(DepartmentDto deptModel) {
		Department dept=new Department();
    	dept.setDepartmentid(deptModel.getId());
    	if(deptModel.getParentId() != null && !deptModel.getParentId().equals("")){
    		dept.setParentid(deptModel.getParentId());    		
    	}
    	dept.setSort(deptModel.getSort());
    	dept.setDepartmentname(deptModel.getName());
    	dept.setDescription(deptModel.getDesc());
    	dept.setCreateperson(deptModel.getCreatePerson());
    	dept.setUpdateperson(dept.getCreateperson());
    	dept.setCreatedate(new Date());
    	dept.setUpdatedate(dept.getCreatedate());
    	dept.setVersion(1);
    	departmentDao.insertSelective(dept);
		return true;
	}
	@Override
	public boolean deleteByPrimaryKey(String departmentId) {
		DepartmentExample example = new DepartmentExample();
		example.or().andParentidEqualTo(departmentId);
		List<Department> deptList = departmentDao.selectByExampleWithBLOBs(example);
		if(deptList != null && deptList.size() > 0){
			for (Department dept : deptList) {
				this.deleteByPrimaryKey(dept.getDepartmentid());
			}
		}
		departmentUserDao.deleteByDepartmentId(departmentId);
		departmentDao.deleteByPrimaryKey(departmentId);
		return true;
	}
	
	@Override
	public boolean update(DepartmentDto model) {
		Department dept = new Department();
		dept.setDepartmentid(model.getId());
		if(model.getName() != null && !model.getName().equals("")){
			dept.setDepartmentname(model.getName());			
		}
//		dept.setSort(model.getSort());
		if(model.getDesc() != null){
			dept.setDescription(model.getDesc());			
		}
//		dept.setUpdateperson(model.getUpdatePerson());
		dept.setUpdatedate(new Date());
		dept.setParentid(model.getParentId());
		System.out.println(dept.toString());
		departmentDao.updateByPrimaryKeySelective(dept);
		return false;
	}

	@Override
	public DepartmentDto getByDeptIdToModel(String departmentId) {
		if(departmentId == null || departmentId.equals("")){
			return null;
		}
		Department dept = departmentDao.selectByPrimaryKey(departmentId);
		if(dept != null){
			return this.toDepartmentModel(dept);
		}
		return null;
	}

	@Override
	public boolean deleteUserGroup(String userid, String groupid) {
		DepartmentUserKey key = new DepartmentUserKey();
		key.setUserid(userid);
		key.setDepartmentid(groupid);
		departmentUserDao.deleteByPrimaryKey(key);
		return false;
	}

	@Override
	public List<DepartmentDto> getChildrenByDepartmentId(String id) {
		DepartmentExample example = new DepartmentExample();
		if (StringUtils.isNotBlank(id)) {
			example.or().andParentidEqualTo(id);
		}else{
			example.or().andParentidIsNull();
		}
		example.setOrderByClause("CreateDate");
		List<Department> list = departmentDao.selectByExampleWithBLOBs(example);
		List<DepartmentDto> departments = new ArrayList<DepartmentDto>();
		if(list != null && list.size() > 0){
			for (Department department : list) {
				DepartmentDto dept = treeGrid(department, true);
				departments.add(dept);
			}			
		}
		return departments;
	}
	@Override
	public List<EasyuiTreeNode> tree(String id) {
		DepartmentExample example = new DepartmentExample();
		if (StringUtils.isNotBlank(id)) {
			example.or().andParentidEqualTo(id);
		}else{
			example.or().andParentidIsNull();
		}
		example.setOrderByClause("CreateDate");
		List<Department> list = departmentDao.selectByExampleWithBLOBs(example);
		List<EasyuiTreeNode> treeNodes = new ArrayList<EasyuiTreeNode>();
		if(list != null && list.size() > 0){
			for (Department department : list) {
				EasyuiTreeNode node = tree(department, true);
				treeNodes.add(node);
			}			
		}
		return treeNodes;
	}
	/**
	 * 递归辅助函数
	 * @param department
	 * @param recursive
	 * @return
	 */
	private DepartmentDto treeGrid(Department department, boolean recursive) {
		DepartmentDto departmentModel = this.toDepartmentModel(department);
		DepartmentExample example = new DepartmentExample();
		example.setOrderByClause("CreateDate");
		example.or().andParentidEqualTo(department.getDepartmentid());
		List<Department> list = departmentDao.selectByExampleWithBLOBs(example);
		if (list != null && list.size() > 0) {
			if (recursive) {// 设置是否循环
				List<DepartmentDto> children = new ArrayList<DepartmentDto>();
//				Collections.sort(list, new DeparmentComparator());// 按sort排序
				for (Department d : list) {
					DepartmentDto dept = treeGrid(d, true);
					children.add(dept);
				}
				departmentModel.setChildren(children);
			}
		}
		return departmentModel;
	}

	/**
	 * 递归辅助函数
	 * @param department
	 * @param recursive
	 * @return
	 */
	private EasyuiTreeNode tree(Department department, boolean recursive) {
		EasyuiTreeNode node = new EasyuiTreeNode();
		node.setId(department.getDepartmentid());
		node.setText(department.getDepartmentname());
		DepartmentExample example = new DepartmentExample();
		example.or().andParentidEqualTo(department.getDepartmentid());
		List<Department> list = departmentDao.selectByExampleWithBLOBs(example);
		if (list != null && list.size() > 0) {
			if (recursive) {// 设置是否循环
				List<EasyuiTreeNode> children = new ArrayList<EasyuiTreeNode>();
//				Collections.sort(departmentList, new DeparmentComparator());// 按sort排序
				for (Department d : list) {
					EasyuiTreeNode dept = tree(d, true);
					children.add(dept);
				}
				node.setChildren(children);
			}
		}
		return node;
	}
	@Override
	public DepartmentDto getRootDepartmentModel() {
		Department dept = departmentDao.selectByParentIdIsNull();
		if(dept != null){
			return this.toDepartmentModel(dept);
		}
		return null;
	}
	@Override
	public String getDeptIdByUserId(String userId) {
		DepartmentUser deptUser = departmentUserDao.selectByUserId(userId);
		if(deptUser != null){
			return deptUser.getDepartmentid();
		}
		return null;
	}
	DepartmentDto toDepartmentModel(Department department) {
		DepartmentDto deptModel = new DepartmentDto();
    	deptModel.setId(department.getDepartmentid());
    	deptModel.setParentId(department.getParentid());
    	deptModel.setSort(department.getSort());
    	deptModel.setName(department.getDepartmentname());
    	deptModel.setDesc(department.getDescription());
    	deptModel.setCreatePerson(department.getCreateperson());
    	deptModel.setCreateTime(DateUtil.dateFormat(department.getCreatedate()));
    	deptModel.setUpdateTime(DateUtil.dateFormat(department.getUpdatedate()));
    	deptModel.setUpdatePerson(department.getUpdateperson());
    	return deptModel;
    }
    Department toDepartment(DepartmentDto deptModel) {
    	Department dept=new Department();
    	dept.setDepartmentid(deptModel.getId());
    	dept.setParentid(deptModel.getParentId());
    	dept.setSort(deptModel.getSort());
    	dept.setDepartmentname(deptModel.getName());
    	dept.setDescription(deptModel.getDesc());
    	dept.setCreateperson(deptModel.getCreatePerson());
    	return dept;
    }


}
